#!/bin/sh

set -e

RELEASE=${RELEASE:-14.2}
ARCH=${ARCH:-64}
OS=${OS:-Slackware}
BASEDIR=${BASEDIR:-/media/chroot}
if [ "$ARCH" = "64" ]; then
	LIBDIRSUFFIX="64"
else
	LIBDIRSUFFIX=""
fi

TARGETDIR="${TARGETDIR:-$BASEDIR/$OS${LIBDIRSUFFIX}-$RELEASE}"

mount -vt proc proc $TARGETDIR/proc
mount -vt sysfs sysfs $TARGETDIR/sys
# Copy /etc/hosts
/bin/cp -vf /etc/hosts $TARGETDIR/etc/

# For internet access, copy /etc/resolv.conf
/bin/cp -vf /etc/resolv.conf $TARGETDIR/etc/resolv.conf

# Link /etc/mtab
chroot $TARGETDIR rm /etc/mtab 2> /dev/null
chroot $TARGETDIR ln -vs /proc/mounts /etc/mtab

# Prevent D-Bus errors
cp -v /var/lib/dbus/machine-id $TARGETDIR/var/lib/dbus/machine-id

if [ "$ARCH" = "64" ]; then
	chroot $TARGETDIR /bin/sh -l
else
	linux32 chroot $TARGETDIR /bin/sh -l
fi

umount -v $TARGETDIR/proc
umount -v $TARGETDIR/sys
